/**
 * A Linked List
 * @file linked_list.cpp
 * @see http://code.google.com/p/mega8-hob/source/browse/trunk/linked_list.cpp
 * @author Hamish Cumming & Brian Thorne
 * @version see SVN revision number
 */

#include "linked_list.h"

linked_list_t * linked_list_create(void)
{
    linked_list_t * ll;
    ll = (linked_list_t *)malloc(sizeof(linked_list_t));
    /* should check pointer here, or where called */
    ll->start_node = ll->end_node = NULL;
    ll->nodes = 0;
    return ll;
}

list_node_t * add_node (linked_list_t * ll, point_t * point)
{
    //Adds a node to the linked list
    list_node_t * node;
    /* Should check pointer */
    node = (list_node_t * )malloc(sizeof(list_node_t));
    node->next = node->previous = NULL;
    node->point = point;
    if (ll->start_node == NULL)
    {
        ll->start_node = ll->end_node = node;
    } 
    else 
    {
        ll->end_node->next = node;
        node->previous = ll->end_node;
        ll->end_node = node;
    }
    ll->nodes++;
    return node;
}

void delete_node(linked_list_t * ll, list_node_t * node)
{
    if (node->previous != NULL)
    {
        node->previous->next = node->next;
    } 
    else
    {
        ll->start_node = node->next;
    }
    if (node->next != NULL)
    {
        node->next->previous = node->previous;
    }
    else
    {
        ll->end_node = node->previous;
    }
    ll->nodes--;
    /* This assumes we want to free node->data too */
    free(node->point);
    free(node);
}
